﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Client.Utility;
using System.Windows.Threading;
using EzDesk.DesktopClient.ServiceLocator.ClientService;
using EzDesk.Generic;
using EzDesk.DesktopClient.Model;
using EzDesk.DesktopClient;
using System.Threading;
using EzDesk.DesktopClient.Event;
using EzDesk.EventAggregation;
using System.Collections.ObjectModel;
using System.Diagnostics;
using EzDesk.Generic.Collections;

namespace EzDesk.DesktopClient
{
    /// <summary>
    /// @Refactory 
    /// This class should be split into 2 classes.
    /// </summary>
    public class UpdateDataManagement : ManagementBase<UpdateDataManagement>
    {
        private DispatcherTimer _disappearTimer = new DispatcherTimer();
        public TimeSpan DurationTime = TimeSpan.FromSeconds(8);
        private bool _isRunning = false;
        private List<Feed> _feeds = new List<Feed>();

        public UserProfile User
        {
            get
            {
                return UserManagement.Instance.LogUser;
            }
        }

        public void Start(int duration)
        {
            DurationTime = TimeSpan.FromSeconds(duration);
            SetDuration();
            this.Start();
        }

        private void SetDuration()
        {
            _disappearTimer.Interval = DurationTime;
        }

        public void Start()
        {
            _disappearTimer.Start();
        }

        public void Stop()
        {
            _disappearTimer.Stop();
        }

        public UpdateDataManagement()
            : base()
        {
            InitTickEvent();
            InitEvent();
        }


        private void InitTickEvent()
        {
            _disappearTimer.Tick += (sender, e) =>
            {
                this.UpdateFeeds();
            };
            SetDuration();
        }

        private void UpdateFeeds()
        {
            if (_feeds.Count == 0) return;
            string ids = this._feeds.Select(f => f.FeedID).Join(",");
            this.ClientService.SetFeedsIsRead(ids, User.Id.ToString(), true);
            lock (this)
            {   
                _feeds.Clear();
            }
        }

        private void InitEvent()
        {
            LoginEvent.Instance.Subscribe(this.OnUserLogin, ThreadOption.BackgroundThread);
            LogoutEvent.Instance.Subscribe(this.OnUserLogout, ThreadOption.BackgroundThread);
            SetAsReadEvent.Instance.Subscribe(this.FeedSetAsRead, ThreadOption.BackgroundThread);
        }

        private void OnUserLogin(LoginArgs args)
        {
            args.IsSuccess.TrueDo(this.Start);
        }

        private void OnUserLogout(LogoutArgs args)
        {
            this.Stop();
        }

        public void FeedSetAsRead(Feed feedModel)
        {
            if (feedModel.IsRead.HasValue && !feedModel.IsRead.Value)
            {
                lock (this)
                {
                    this._feeds.Add(feedModel);
                }
            }
        }
    }
}
